package com.nlx.notes.module.controller;

import com.nlx.notes.module.bean.dto.FlowPageQueryDTO;
import com.nlx.notes.module.bean.dto.context.UserContext;
import com.nlx.notes.module.bean.dto.flow.FlowEditDTO;
import com.nlx.notes.module.bean.vo.DayFlowViewVo;
import com.nlx.notes.module.bean.vo.FlowOverviewVo;
import com.nlx.notes.module.bean.vo.PageResultVo;
import com.nlx.notes.module.bean.vo.ResultVO;
import com.nlx.notes.module.bean.vo.flow.FlowDetailVO;
import com.nlx.notes.module.bean.vo.flow.FlowQueryOverviewVO;
import com.nlx.notes.module.bean.vo.flow.FlowRecordVO;
import com.nlx.notes.module.core.util.JSONUtils;
import com.nlx.notes.module.service.flow.FlowManageService;
import com.nlx.notes.support.context.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping(value = "/flow")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class FlowManageController {


    final FlowManageService flowManageService;

    /**
     * 流水总览
     * @return FlowOverviewVo
     */
    @GetMapping(value = "/overview")
    public ResultVO flowOverview(){
        return ResultVO.success(flowManageService.flowOverview(UserHolder.getUserContext().getId(),UserHolder.getUserContext().getBookId()));
    }

    @GetMapping(value = "/v1/record/overview")
    public FlowQueryOverviewVO flowQueryOverview(FlowPageQueryDTO flowPageQueryDTO){
        flowPageQueryDTO.setUserId(UserHolder.getUserContext().getId());
        flowPageQueryDTO.setBookId(UserHolder.getUserContext().getBookId());
        log.info("查询流水中的概览数据，入参：{}",JSONUtils.toString(flowPageQueryDTO));
        return flowManageService.flowQueryOverview(flowPageQueryDTO);
    }

    /**
     * 流水列表分页
     * @param flowPageQueryDTO 查询参数
     * @return DayFlowViewVo
     */
    @GetMapping(value = "/pages")
    public PageResultVo<DayFlowViewVo> flowPage(FlowPageQueryDTO flowPageQueryDTO) {
        flowPageQueryDTO.setUserId(UserHolder.getUserContext().getId());
        flowPageQueryDTO.setBookId(UserHolder.getUserContext().getBookId());
        log.info("查询流水分页数据，参数：{}", JSONUtils.toString(flowPageQueryDTO));
        return flowManageService.flowPage(flowPageQueryDTO);
    }


    /**
     * 删除流水
     * @param id 流水id
     * @return boolean
     */
    @PostMapping("/delete/{id}")
    public boolean deleteFlow(@PathVariable("id") Long id){
        log.info("删除流水数据，id：{}",id);
        return flowManageService.deleteFlow(id,UserHolder.getUserContext().getId());
    }

    /**
     * 获取流水的详情
     * @param flowId 流水id
     * @return FlowDetailVO
     */
    @GetMapping("/detail")
    public FlowDetailVO detail(@RequestParam(value = "flowId") Long flowId){
        log.info("获取流水 {} 的详情，用户：{}",flowId,UserHolder.getUserContext().getUserPhone());
        return flowManageService.detail(flowId,UserHolder.getUserContext().getId());
    }

    /**
     * 修改流水
     * @return
     */
    @PostMapping("/v1/edit")
    public FlowDetailVO edit(@RequestBody FlowEditDTO flowEditDTO){
        log.info("编辑用户流水，用户：{}",JSONUtils.toString(flowEditDTO),UserHolder.getUserContext().getUserPhone());
        flowEditDTO.setUserId(UserHolder.getUserContext().getId());
        return flowManageService.edit(flowEditDTO);
    }


    @GetMapping(value = "/v1/flow-day")
    public DayFlowViewVo flowDay(@RequestParam("dayDate") String dayDate) {
        log.info("查询用户某天的流水信息，日期：{},用户：{}", dayDate, UserHolder.getUserContext().getUserPhone());
        return flowManageService.findFlowDay(dayDate,UserHolder.getUserContext().getBookId(), UserHolder.getUserContext().getId());
    }

    /**
     * 查询某一月的流水数据
     * @param month 月份
     * @return DayFlowViewVo
     */
    @GetMapping(value = "/v1/flow-month")
    public List<DayFlowViewVo> flowMonth(@RequestParam("dayDate") String month){
        log.info("查询用户某月的流水信息，日期：{},用户：{}",month,UserHolder.getUserContext().getUserPhone());
        return flowManageService.findFlowMonth(month,UserHolder.getUserContext().getBookId(),UserHolder.getUserContext().getId());
    }


}
